package com.syscom.common.utils.generator.tool;

import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @author wjz
 * @date 2021/1/4 12:09
 * @Description
 */
@Component
public class GeneratorTool {

    @Value("${spring.datasource.url}")
    private String url;

    @Value("${spring.datasource.driverClassName}")
    private String driverClassName;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;


    /**
     * 数据源配置
     */
    public DataSourceConfig getDataSourceConfig() {

        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl(url);
        // dsc.setSchemaName("public");
        dsc.setDriverName(driverClassName);
        dsc.setUsername(username);
        dsc.setPassword(password);
        return dsc;
    }

    /**
     * 全局配置
     */
    public GlobalConfig getGlobalConfig(String projectPath, String outPutDir) {

        GlobalConfig gc = new GlobalConfig();

        gc.setOutputDir(projectPath + "/" + outPutDir + "/src/main/java");
        //作者
        gc.setAuthor("wjz");
        //生成后是否打开资源管理器
        gc.setOpen(false);
        //重新生成时文件是否覆盖
//        gc.setFileOverride(true);
        //开启Swagger2模式
        gc.setSwagger2(true);
        //自定义文件命名，注意 %s 会自动填充表实体属性
//        gc.setServiceName("%sService");
        return gc;
    }

    /**
     * 包配置
     */
    public PackageConfig getPackageConfig(String moduleName, String parent) {
        // 包配置
        PackageConfig pc = new PackageConfig();
//        pc.setModuleName(scanner("模块名"));
        //默认所有的文件生成在  common模组里
        pc.setModuleName(moduleName);
        pc.setParent(parent);
        return pc;
    }

    /**
     * 策略配置
     */
    public StrategyConfig getStrategyConfig(String tableName) {
        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setSuperEntityClass("com.syscom.common.base.BaseDefaultId");
        strategy.setEntityLombokModel(true);
//        strategy.setSuperControllerClass("com.baomidou.mybatisplus.samples.generator.common.BaseController");
//        strategy.setInclude(scanner("表名(大小写均可)"));
        strategy.setInclude(tableName);
        strategy.setSuperEntityColumns("id");
        strategy.setControllerMappingHyphenStyle(true);
        //restful api风格控制器
        strategy.setRestControllerStyle(true);
        return strategy;
    }
}
